package cn.commander.model;

import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;


public class CusPageInfo<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private int currentPage = 1;        //起始页码
    private int pageSize = 10;           //每页记录数
    private long totalSize;         //总记录数
    private int totalPage;          //总页数
    private boolean isFirstPage;    //是否为首页
    private boolean isLastPage;     //是否为末页
    private boolean hasPreviousPage;    //是否有上一页
    private boolean hasNextPage;        //是否有下一页

    public CusPageInfo() {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
    }

    public void handleData (List<T> list) {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
        if(list instanceof Page) {
            Page page = (Page)list;
            this.currentPage = page.getPageNum();
            this.totalPage = page.getPages();
            this.totalSize = page.getTotal();

        } else if(list instanceof Collection) {
            this.currentPage = 1;
            this.totalPage = 1;
            this.totalSize = (long)list.size();
        }

        if(list instanceof Collection) {
            this.judgePageBoudary();
        }
    }


    private void judgePageBoudary() {
        if (totalSize == 0) {
            this.isFirstPage = true;    // 查询没数据，设置首页不能点击的状态
            this.isLastPage = true;     // 查询没数据，设置最后一页不能点击的状态
        } else {
            this.isFirstPage = this.currentPage == 1;
            this.isLastPage = this.currentPage == this.totalPage;
        }

        this.hasPreviousPage = this.currentPage > 1 && this.currentPage <= this.totalPage;
        this.hasNextPage = this.currentPage < this.totalPage;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public boolean isFirstPage() {
        return isFirstPage;
    }

    public void setFirstPage(boolean firstPage) {
        isFirstPage = firstPage;
    }

    public boolean isLastPage() {
        return isLastPage;
    }

    public void setLastPage(boolean lastPage) {
        isLastPage = lastPage;
    }

    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer("CusPageInfo{");
        sb.append("currentPage=").append(this.currentPage);
        sb.append(", pageSize=").append(this.pageSize);
        sb.append(", totalSize=").append(this.totalSize);
        sb.append(", totalPage=").append(this.totalPage);
        sb.append(", isFirstPage=").append(this.isFirstPage);
        sb.append(", isLastPage=").append(this.isLastPage);
        sb.append(", hasPreviousPage=").append(this.hasPreviousPage);
        sb.append(", hasNextPage=").append(this.hasNextPage);
        sb.append('}');
        return sb.toString();
    }
}
